package com.oa.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oa.dao.ReplyDao;
import com.oa.entity.Forum;
import com.oa.entity.Reply;
import com.oa.entity.Topic;
import com.oa.service.ReplyService;

@Service("replyService")
@Transactional
public class ReplyServiceImpl implements ReplyService {
	// 通过注解把replyDao注入进来
	@Resource(name = "replyDao")
	private ReplyDao replyDao;

	public boolean addReply(Reply reply) {
		reply.setPostingTime(new Date());
		replyDao.saveEntity(reply);
		Topic topic = reply.getTopic();
		topic.setReplyCount(topic.getReplyCount() + 1);
		topic.setLastReply(reply);
		topic.setLastUpdateTime(reply.getPostingTime());
		Forum forum = topic.getForum();
		forum.setArticleCount(forum.getArticleCount() + 1);
		replyDao.updateReply2(topic);
		return replyDao.updateReply2(forum);

	}

	public boolean updateReply(Reply reply) {
		return replyDao.updateEntity(reply);
	}

	public boolean deleteReply(Reply reply) {
		return replyDao.deleteEntity(reply);
	}

	public List<Reply> selectDetailTopic(String topicId) {
		List<Reply> replyList = replyDao.queryDetailTopic(topicId);
		return replyList;
	}

	public Reply selectReply(String replyId) {
		Reply reply = replyDao.loadEntity(replyId);
		return reply;
	}

	public Reply selectLastReply(String topicId) {
		Reply reply = replyDao.queryLastReply(topicId);
		return reply;
	}

	public ReplyDao getReplyDao() {
		return replyDao;
	}

	public void setReplyDao(ReplyDao replyDao) {
		this.replyDao = replyDao;
	}

}
